<?php

namespace app\api\controller\store;

use app\admin\model\Brand;
use app\admin\model\Financialrecords;
use app\admin\model\join\Merchant;
use app\admin\model\store\Bank;
use app\admin\model\store\Category;
use app\admin\model\store\Withdrawal;
use app\admin\model\Years;
use app\api\model\GoodsProduct;
use app\api\model\GoodsSpecification;
use app\api\model\JoinMerchant;
use app\api\model\User;
use app\common\controller\Api;
use think\Request;


class Product extends Api
{
    protected $noNeedRight = '*';
    protected $noNeedLogin = ['store_index','store_settings','store_information','login_passwords'];
    protected $store_id;


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
//        var_dump($this->auth->id);die;
        $this->store_id=JoinMerchant::where(['user_id'=>$this->auth->id])->value('id');
//
    }

    /**
     * 商家商品列表
     * store/product/store_product
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function store_product(Request $request){
        $post=$request->post();
        $where['join_merchant_id']=$this->store_id;
        if($post['is_on_sale']==1){
            $where['is_on_sale']=1;
        }
        if($post['is_on_sale']==0){
            $where['is_on_sale']=0;
        }
        $post['pagesize']?$post['pagesize']=$post['pagesize']:$post['pagesize']=10;
        if($post['type']==1){
            $where['product_type']=1;
            $store_list=\app\admin\model\store\Goods::where($where)
                ->field('id,goods_image,name,sales,specification,is_on_sale')
                ->page($post['page'],$post['pagesize'])
                ->order('createtime desc')
                ->select();
            if($store_list){

                $store_list=json_array($store_list);
                 foreach ($store_list as &$value){
                     $value['goods_image']=yuming($value['goods_image']);
                }
            }

        }
        if($post['type']==2){
            $where['product_type']=2;
            $store_list=\app\admin\model\store\Goods::where($where)
                ->field('id,goods_image,name,sales,stock,day_money,moth_money,frame_number,license_plate_number,is_on_sale')
                ->page($post['page'],$post['pagesize'])
                ->order('createtime desc')
                ->select();
            if($store_list){
                $store_list=json_array($store_list);
                foreach ($store_list as &$value){
                    $value['goods_image']=yuming($value['goods_image']);
                }
            }
        }
        if($post['type']==3){
            $where['product_type']=3;
            $store_list=\app\admin\model\store\Goods::where($where)
                ->field('id,goods_image,name,sales,specification,is_on_sale')
                ->page($post['page'],$post['pagesize'])
                ->order('createtime desc')
                ->select();
            if($store_list){
                $store_list=json_array($store_list);
                foreach ($store_list as &$value){
                    $value['goods_image']=yuming($value['goods_image']);
                }
            }
        }
        return json(['code'=>200,'msg'=>'请求成功','data'=>$store_list]);
    }

    /**
     * 删除商品
     * store/product/del_product
     * @param Request $request
     * @return \think\response\Json
     */
    public function  del_product(Request $request)
    {
        $post = $request->post();
        $store_del = \app\admin\model\store\Goods::where(['id' => $post['id']])->delete();
        if($store_del){
            return json(['code'=>200,'msg'=>'已删除','data'=>'']);
        }else{
            return json(['code'=>404,'msg'=>'删除失败,请稍后再试','data'=>'']);
        }
    }

    /**
     * 商品上下架
     * store/product/on_sale
     * @param Request $request
     * @return \think\response\Json
     */
    public function  on_sale(Request $request)
    {
        $post = $request->post();
        if($post['type']==1){
            $on_sale = \app\admin\model\store\Goods::where(['id' => $post['id']])->update(['is_on_sale'=>1]);
            $msg='已上架';
            $msg_erro='上架失败，请稍后再试';
        }
        if($post['type']==0){
            $on_sale = \app\admin\model\store\Goods::where(['id' => $post['id']])->update(['is_on_sale'=>0]);
            $msg='已下架';
            $msg_erro='下架失败，请稍后再试';
        }
        if($on_sale){
            return json(['code'=>200,'msg'=>$msg,'data'=>'']);
        }else{
            return json(['code'=>404,'msg'=>$msg_erro,'data'=>'']);
        }
    }

    /**
     * 商品分类
     * store/product/store_category
     * @return \think\response\Json
     * @throws \think\exception\DbException
     *
     */
    public  function store_category(){

        $store_category=Category::all();
        return json(['code'=>404,'msg'=>'请求成功','data'=>$store_category]);

    }

    /**
     * 添加商品
     * store/product/add_product
     * @param Request $request
     * @return \think\response\Json
     *
     */
    public  function add_product(Request $request){
        $post=$request->post();
        $data=[
             'product_type'=>1,
             //商品名称
             'name'=>$post['name'],
             //商品类型
             'category_id'=>$post['category_id'],
             //商品轮播图
             'gallery_images'=>$post['gallery_images'],
             //商品规格
             'specification'=>json_encode($post['specification']),
             //销售价格
             'retail_price'=>$post['retail_price'],
             //商品库存
             'stock'=>$post['stock'],
             //商品状态
             'is_on_sale'=>$post['is_on_sale'],
             //七天无理由
             'is_seven_days'=>$post['is_seven_days'],
             //商品详情
             'details_content'=>$post['details_content'],
             'createtime'=>time()
        ];
        $Goods=\app\admin\model\store\Goods::create($data);
        if($Goods){
            foreach ($post['specification'] as &$value){
                 $GoodsProduct=GoodsProduct::create(['name'=>$value['name'],'store_goods_id'=>$Goods->id]);

                foreach ($value['ch'] as &$values){
                    GoodsSpecification::create([
                        'specification'=>$values['specification'],
                        'num'=>$values['num'],
                        'money'=>$values['money'],
                        'pic_image'=>$values['pic_image'],
                        'store_goods_id'=>$Goods->id,
                        'goods_product_id'=>$GoodsProduct->id
                    ]);
               }
            }
            return json(['code'=>200,'msg'=>'添加成功','data'=>'']);
        }else{
            return json(['code'=>404,'msg'=>'添加失败','data'=>'']);
        }
    }

    /**
     * 商品编辑回显
     * store/product/edit_product_hx
     * @param Request $request
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     */
    public  function  edit_product_hx(Request $request)
    {
        $post = $request->post();

        $Goods = \app\admin\model\store\Goods::where(['id' => $post['id']])
            ->field('id,name,category_id,gallery_images,specification,retail_price,stock,is_on_sale,is_seven_days,details_content')
            ->find()
            ->toArray();

        $GoodsProduct = json_decode(json_encode(GoodsProduct::where(['store_goods_id' => $post['id']])->field('id,name')->select()),1);
//        var_dump($GoodsProduct);die;
        $GoodsSpecification = json_decode(json_encode(GoodsSpecification::where(['store_goods_id' => $post['id']])
            ->field('id,specification,pic_image,num,money,goods_product_id')->select()),1);
        foreach ($GoodsProduct as &$value){
            $value['ch']=[];
            foreach ($GoodsSpecification as &$values){
                 if($value['id']==$values['goods_product_id']){
                     $value['ch'][]=$values;
                 }
            }
        }
        $Goods['specification']=$GoodsProduct;
        unset($Goods['is_hot_text'],$Goods['type_text'],$Goods['goods_type_text'],$Goods['is_seven_days_text'],$Goods['is_on_sale_text']);
        return json(['code'=>200,'msg'=>'请求成功','data'=>$Goods]);
    }

    /**
     * 修改商品信息
     * store/product/edit_product
     * @param Request $request
     * @return \think\response\Json
     *
     */
    public function edit_product(Request $request){

        $post=$request->post();
        $data=[
            'product_type'=>1,
            //商品名称
            'name'=>$post['name'],
            //商品类型
            'category_id'=>$post['category_id'],
            //商品轮播图
            'gallery_images'=>$post['gallery_images'],
            //商品规格
            'specification'=>json_encode($post['specification']),
            //销售价格
            'retail_price'=>$post['retail_price'],
            //商品库存
            'stock'=>$post['stock'],
            //商品状态
            'is_on_sale'=>$post['is_on_sale'],
            //七天无理由
            'is_seven_days'=>$post['is_seven_days'],
            //商品详情
            'details_content'=>$post['details_content'],
        ];
        $Goods=\app\admin\model\store\Goods::where(['id'=>$post['id']])->update($data);
        if($Goods){
            foreach ($post['specification'] as &$value){
                $GoodsProduct=GoodsProduct::where(['id'=>$value['id']])->update(['name'=>$value['name']]);

                foreach ($value['ch'] as &$values){
                    GoodsSpecification::where(['id'=>$values['id']])->update([
                        'specification'=>$values['specification'],
                        'num'=>$values['num'],
                        'money'=>$values['money'],
                        'pic_image'=>$values['pic_image'],
                        'store_goods_id'=>$Goods->id,
//                        'goods_product_id'=>$GoodsProduct->id
                    ]);
                }
            }
            return json(['code'=>200,'msg'=>'修改成功','data'=>'']);
        }else{
            return json(['code'=>404,'msg'=>'添加失败','data'=>'']);
        }
    }


    /**
     * 添加租车商品
     * store/product/add_zc
     * @param Request $request
     * @return \think\response\Json
     *
     */
    public  function add_zc(Request $request){
        $post=$request->post();
        $data=[
            'product_type'=>2,
            //车牌号
            'license_plate_number'=>$post['license_plate_number'],
            //车架号
            'frame_number'=>$post['frame_number'],
            //年限
            'years_id'=>$post['years_id'],
            //品牌
            'brand_id'=>$post['brand_id'],
            //商品大图
            'goods_image'=>$post['goods_image'],
            //商品库存
            'stock'=>$post['stock'],
            //商品状态
            'is_on_sale'=>$post['is_on_sale'],
            //日租价
            'day_money'=>$post['day_money'],
            //月租价
            'moth_money'=>$post['moth_money'],
            //押金
            'deposit'=>$post['deposit'],
            'createtime'=>time()
        ];
        $Goods=\app\admin\model\store\Goods::create($data);
        if($Goods){
            return json(['code'=>200,'msg'=>'添加成功','data'=>'']);
        }else{
            return json(['code'=>404,'msg'=>'添加失败','data'=>'']);
        }
    }

    /**
     * 租车商品编辑回显
     * store/product/edit_product_hx
     * @param Request $request
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     */
    public  function  edit_zc_hx(Request $request)
    {
        $post = $request->post();


        $Goods = \app\admin\model\store\Goods::where(['id' => $post['id']])
            ->field('id,license_plate_number,frame_number,years_id,brand_id,goods_image,stock,is_on_sale,day_money,moth_money,deposit')
            ->find()
            ->toArray();
        unset($Goods['is_hot_text'],$Goods['type_text'],$Goods['goods_type_text'],$Goods['is_seven_days_text'],$Goods['is_on_sale_text']);
        return json(['code'=>200,'msg'=>'请求成功','data'=>$Goods]);
    }

    /**
     * 修改租车商品信息
     * store/product/edit_zc
     * @param Request $request
     * @return \think\response\Json
     *
     */
    public function edit_zc(Request $request){

        $post=$request->post();
        $data=[
            'product_type'=>2,
            //车牌号
            'license_plate_number'=>$post['license_plate_number'],
            //车架号
            'frame_number'=>$post['frame_number'],
            //年限
            'years_id'=>$post['years_id'],
            //品牌
            'brand_id'=>$post['brand_id'],
            //商品大图
            'goods_image'=>$post['goods_image'],
            //商品库存
            'stock'=>$post['stock'],
            //商品状态
            'is_on_sale'=>$post['is_on_sale'],
            //日租价
            'day_money'=>$post['day_money'],
            //月租价
            'moth_money'=>$post['moth_money'],
            //押金
            'deposit'=>$post['deposit'],
        ];
        $Goods=\app\admin\model\store\Goods::where(['id'=>$post['id']])->update($data);
        if($Goods){

            return json(['code'=>200,'msg'=>'修改成功','data'=>'']);
        }else{
            return json(['code'=>404,'msg'=>'添加失败','data'=>'']);
        }
    }



    /**
     * 添加电池商品
     * store/product/add_dc
     * @param Request $request
     * @return \think\response\Json
     *
     */
    public  function add_dc(Request $request){
        $post=$request->post();
        $data=[
            'product_type'=>3,
            //商品名称
            'name'=>$post['name'],
//            //商品类型
//            'category_id'=>$post['category_id'],
            //商品轮播图
            'gallery_images'=>$post['gallery_images'],
            //商品规格
            'specification'=>json_encode($post['specification']),
            //销售价格
            'retail_price'=>$post['retail_price'],
            //商品库存
            'stock'=>$post['stock'],
            //商品状态
            'is_on_sale'=>$post['is_on_sale'],
//            //七天无理由
//            'is_seven_days'=>$post['is_seven_days'],
            //商品详情
            'details_content'=>$post['details_content'],
            'createtime'=>time()
        ];
        $Goods=\app\admin\model\store\Goods::create($data);
        if($Goods){
            foreach ($post['specification'] as &$value){
                $GoodsProduct=GoodsProduct::create(['name'=>$value['name'],'store_goods_id'=>$Goods->id]);

                foreach ($value['ch'] as &$values){
                    GoodsSpecification::create([
                        'specification'=>$values['specification'],
                        'num'=>$values['num'],
                        'money'=>$values['money'],
                        'pic_image'=>$values['pic_image'],
                        'store_goods_id'=>$Goods->id,
                        'goods_product_id'=>$GoodsProduct->id
                    ]);
                }
            }
            return json(['code'=>200,'msg'=>'添加成功','data'=>'']);
        }else{
            return json(['code'=>404,'msg'=>'添加失败','data'=>'']);
        }
    }

    /**
     * 电池商品编辑回显
     * store/product/edit_dc_hx
     * @param Request $request
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     */
    public  function  edit_dc_hx(Request $request)
    {
        $post = $request->post();

        $Goods = \app\admin\model\store\Goods::where(['id' => $post['id']])
            ->field('id,name,gallery_images,specification,retail_price,stock,is_on_sale,details_content')
            ->find()
            ->toArray();

        $GoodsProduct = json_decode(json_encode(GoodsProduct::where(['store_goods_id' => $post['id']])->field('id,name')->select()),1);
//        var_dump($GoodsProduct);die;
        $GoodsSpecification = json_decode(json_encode(GoodsSpecification::where(['store_goods_id' => $post['id']])
            ->field('id,specification,pic_image,num,money,goods_product_id')->select()),1);
        foreach ($GoodsProduct as &$value){
            $value['ch']=[];
            foreach ($GoodsSpecification as &$values){
                if($value['id']==$values['goods_product_id']){
                    $value['ch'][]=$values;
                }
            }
        }
        $Goods['specification']=$GoodsProduct;
        unset($Goods['is_hot_text'],$Goods['type_text'],$Goods['goods_type_text'],$Goods['is_seven_days_text'],$Goods['is_on_sale_text']);
        return json(['code'=>200,'msg'=>'请求成功','data'=>$Goods]);
    }

    /**
     * 修改电池商品信息
     * store/product/edit_dc
     * @param Request $request
     * @return \think\response\Json
     *
     */
    public function edit_dc(Request $request){

        $post=$request->post();
        $data=[
            'product_type'=>3,
            //商品名称
            'name'=>$post['name'],
//            //商品类型
//            'category_id'=>$post['category_id'],
            //商品轮播图
            'gallery_images'=>$post['gallery_images'],
            //商品规格
            'specification'=>json_encode($post['specification']),
            //销售价格
            'retail_price'=>$post['retail_price'],
            //商品库存
            'stock'=>$post['stock'],
            //商品状态
            'is_on_sale'=>$post['is_on_sale'],
//            //七天无理由
//            'is_seven_days'=>$post['is_seven_days'],
            //商品详情
            'details_content'=>$post['details_content'],
        ];
        $Goods=\app\admin\model\store\Goods::where(['id'=>$post['id']])->update($data);
        if($Goods){
            foreach ($post['specification'] as &$value){
                $GoodsProduct=GoodsProduct::where(['id'=>$value['id']])->update(['name'=>$value['name']]);

                foreach ($value['ch'] as &$values){
                    GoodsSpecification::where(['id'=>$values['id']])->update([
                        'specification'=>$values['specification'],
                        'num'=>$values['num'],
                        'money'=>$values['money'],
                        'pic_image'=>$values['pic_image'],
                        'store_goods_id'=>$Goods->id,
//                        'goods_product_id'=>$GoodsProduct->id
                    ]);
                }
            }
            return json(['code'=>200,'msg'=>'修改成功','data'=>'']);
        }else{
            return json(['code'=>404,'msg'=>'添加失败','data'=>'']);
        }
    }

    /**
     * 年限
     * store/product/years
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function years(Request  $request)
    {
         $post=$request->post();
         $Years=json_array(Years::where(['switch'=>1])->field('id,title')->select());

         return json(['code'=>200,'msg'=>'请求成功','data'=>$Years]);

    }

    /**
     * 品牌
     * store/product/brand
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function brand(Request  $request)
    {
        $post=$request->post();
        $Brand=json_array(Brand::where(['switch'=>1])->field('id,title')->select());

        return json(['code'=>200,'msg'=>'请求成功','data'=>$Brand]);

    }



}